package com.ui.basic.service.administrative;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.ComboBoxPopulator;
import com.common.basic.util.Globals;
import com.ui.basic.assembler.administrative.CustomerAssembler;
import com.ui.basic.command.administrative.CommandCustomer;
import com.ui.basic.ibatis.dao.admin.TblACustomerDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMCountryDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMIdentificationDAO;
import com.ui.basic.ibatis.model.admin.TblACustomer;
import com.ui.basic.ibatis.model.admin.TblACustomerExample;
import com.ui.basic.ibatis.model.masterdata.TblMCountryExample;
import com.ui.basic.ibatis.model.masterdata.TblMIdentificationExample;

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(CustomerServiceImpl.class);

	@Autowired
	private TblACustomerDAO tblACustomerDAO;
	@Autowired
	private TblMCountryDAO tblMCountryDAO;
	@Autowired
	private TblMIdentificationDAO tblMIdentificationDAO;

	public void setTblACustomerDAO(TblACustomerDAO tblACustomerDAO) {
		this.tblACustomerDAO = tblACustomerDAO;
	}

	public void setTblMCountryDAO(TblMCountryDAO tblMCountryDAO) {
		this.tblMCountryDAO = tblMCountryDAO;
	}

	@Transactional(readOnly = true)
	public CommandCustomer getCustomer(CommandCustomer commandCustomer)
			throws SQLException {
		LOGGER.debug(">>>CustomerServiceImpl.getCustomer");
		if (commandCustomer != null) {
			TblACustomerExample example = new TblACustomerExample();

			int rec = 0;
			try {
				rec = tblACustomerDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandCustomer.calculate(rec);
			example.setIntRowNum((commandCustomer.getPgIndex() - 1)
					* commandCustomer.getPgRows());
			example.setIntRowsPerPage(commandCustomer.getPgRows());
			example.setOrderByClause("firstname");
			commandCustomer.setData(tblACustomerDAO.selectByExample(example));
		}
		return commandCustomer;
	}

	@Transactional(readOnly = true)
	public void setCombo(CommandCustomer commandCustomer) throws SQLException {
		LOGGER.debug(">>>CustomerServiceImpl.setCombo");

		TblMCountryExample countryExample = new TblMCountryExample();
		countryExample.setOrderByClause("CODE");
		commandCustomer.setCountryList(ComboBoxPopulator.populate(
				tblMCountryDAO.selectByExample(countryExample), "id",
				new String[] { "code", "name" }, "-"));

		TblMIdentificationExample identificationExample = new TblMIdentificationExample();
		identificationExample.setOrderByClause("CODE");
		commandCustomer.setIdentityList(ComboBoxPopulator.populate(
				tblMIdentificationDAO.selectByExample(identificationExample),
				"id", new String[] { "code", "name" }, "-"));
	}

	public CommandCustomer getCustomerDetail(CommandCustomer commandCustomer)
			throws SQLException {
		LOGGER.debug(">>>CustomerServiceImpl.setCombo");
		commandCustomer.setProperty(tblACustomerDAO
				.selectByPrimaryKey(commandCustomer.getId()));
		return commandCustomer;
	}

	public void save(CommandCustomer commandCustomer) throws SQLException {
		LOGGER.debug(">>>CustomerServiceImpl.save");
		CustomerAssembler assembler = new CustomerAssembler();
		TblACustomer customer = assembler.toEntity(commandCustomer);

		if (commandCustomer.getId() != null) {
			customer.setChangedby((String) (SecurityUtils.getSubject()
					.getSession().getAttribute(Globals.CURRENT_USER_NAME)));
			customer.setChangeddate(new Date());
			try {
				tblACustomerDAO.updateByPrimaryKeySelective(customer);

			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		} else {
			customer.setCreatedby((String) (SecurityUtils.getSubject()
					.getSession().getAttribute(Globals.CURRENT_USER_NAME)));
			customer.setCreateddate(new Date());
			try {
				tblACustomerDAO.insert(customer);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

	public void delete(CommandCustomer commandCustomer) throws SQLException {
		LOGGER.debug(">>>CustomerServiceImpl.delete");
		TblACustomerExample example = new TblACustomerExample();

		if (commandCustomer.getId() != null) {
			example.createCriteria().andIdEqualTo(commandCustomer.getId());
		} else if (commandCustomer.getSelectedRecord() != null
				&& commandCustomer.getSelectedRecord().length > 0) {
			List<Long> idList = new ArrayList<Long>();
			for (String strId : commandCustomer.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}

		tblACustomerDAO.deleteByExample(example);
	}
}
